package com.locatme.fragment;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.locatme.model.Enseigne;
import com.locatme.model.Personne;
import com.locatme.model.Utilisateur;
import com.locatme.modelBD.LangueBD;
import com.locatme.modelBD.LocalisationBD;
import com.locatme.modelBD.UtilisateurBD;
import com.main.locatme.R;

public class HomeFragment extends Fragment implements LocationListener {



    private Utilisateur user;
    public HomeFragment() {
    }

    private TextView txtlabel = null;
    private String type = "";

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public TextView getTxtlabel() {
        return txtlabel;
    }

    public void setTxtlabel(TextView txtlabel) {
        this.txtlabel = txtlabel;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView;

        user = (Utilisateur) getActivity().getIntent().getExtras().getParcelable("user");
        UtilisateurBD userBD = new UtilisateurBD(getActivity());
        LangueBD langueBD = new LangueBD(getActivity());

        if (Personne.class.isAssignableFrom(user.getClass())) {
            userBD.open();
            Personne personne = userBD.verifPersonne(user.getLogin(),user.getPwd());
            userBD.close();
            langueBD.open();
            personne.setMesLangues(langueBD.getLanguageByPersonne(user.getLogin()));
            langueBD.close();
            rootView = inflater.inflate(R.layout.fragment_home, container,false);
            TextView txtlanguep = (TextView) rootView.findViewById(R.id.txtlanguep);
            TextView position = (TextView) rootView.findViewById(R.id.Position);
            txtlanguep.setText(personne.getAffLangues());
            txtlabel = (TextView) rootView.findViewById(R.id.txtLabel);
            txtlabel.setText(personne.getLogin());
            position.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
        double lat=0;
        double lng=0;
                    LocationManager lm = (LocationManager)getActivity().getSystemService(getActivity().LOCATION_SERVICE);
                   /* if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
                    {
                        LocalisationBD locBD = new LocalisationBD(getActivity());
                        locBD.open();
                        int i=locBD.getLocPersonne(user.getLogin());
                        lat = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude();
                        lng = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude();
                        if(i!=0)
                        {
                            locBD.setLoc(i,lat,lng);
                        }
                        else
                        {
                            locBD.insertLocalisation(lat,lng);
                            locBD.setLocPersonne(user.getLogin(),Integer.parseInt(String.valueOf(locBD.getIdLoc())));
                        }
                        locBD.close();
                        Toast.makeText(getActivity(),"Location Update",Toast.LENGTH_LONG).show();
                    }
                    else*/ if(lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
                    {

                        LocalisationBD locBD = new LocalisationBD(getActivity());
                        locBD.open();
                        int i=locBD.getLocPersonne(user.getLogin());
                        try
                        {
                        lat = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getLatitude();
                        lng = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getLongitude();
                        }
                        catch(Exception e)
                        {
                            Toast.makeText(getActivity(),"Problème de localisation, veuillez reesayer ultérieurement",Toast.LENGTH_LONG).show();
                        }

                        if(i!=0)
                        {
                           locBD.setLoc(i,lat,lng);
                            Toast.makeText(getActivity(),"Location Update",Toast.LENGTH_LONG).show();
                        }
                        else
                        {
                        locBD.insertLocalisation(lat,lng);
                        locBD.setLocPersonne(user.getLogin(),Integer.parseInt(String.valueOf(locBD.getIdLoc())));
                            Toast.makeText(getActivity(),"Location Update",Toast.LENGTH_LONG).show();
                        }
                        locBD.close();

                    }

                    //Toast.makeText(getActivity(),"Lat : "+Double.toString(lat)+"Long : "+Double.toString(lng),Toast.LENGTH_LONG).show();
                }
            });
        } else {

            userBD.open();
            Enseigne enseigne = userBD.VerifEnseigne(user.getLogin(), user.getPwd());
            userBD.close();
            langueBD.open();
            enseigne.setMesLangues(langueBD.getLanguageByEnseigne(user.getLogin()));
            langueBD.close();
            rootView = inflater.inflate(R.layout.fragment_home_enseigne,container, false);
            TextView txtnomenseigne = (TextView) rootView.findViewById(R.id.labelnomenseigne);
            TextView txtdescenseigne = (TextView) rootView.findViewById(R.id.txtdescenseigne);
            TextView txthouverture = (TextView) rootView.findViewById(R.id.txthouverture);
            TextView txthfermeture = (TextView) rootView.findViewById(R.id.txthfermeture);
            TextView txtlangueE = (TextView) rootView.findViewById(R.id.txtlangueE);
            txtnomenseigne.setText(enseigne.getNomEnseigne());
            txtdescenseigne.setText(enseigne.getDescEnseigne());
            txthouverture.setText(enseigne.getHeureOuverture());
            txthfermeture.setText(enseigne.getHeureFermeture());
            txtlangueE.setText(enseigne.getAffLangues());
        }

        return rootView;
    }

    @Override
    public void onLocationChanged(Location location) {

    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }
}
